<template>
  <base-panel :show.sync="advanceFlag" width="420px">
    <h3 slot="title">
      <span>{{ $t('component.advancedSearch.title') }}</span>
    </h3>
    <el-form
      :model="form"
      class="advanced-search-form"
      onsubmit="return false;"
      
      label-position="top"
    >
      <el-form-item :label="`${$t('product.type.qrcodeId')}：`">
        <el-col :span="20">
          <el-input v-model="form.qrcodeId" @change="inputChange"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item :label="`${$t('common.base.product')}：`">
        <el-col :span="20">
          <qrcode-select-product
            ref="selectProductRef"
            @input="selectProduct"
            :selectedProduct="selectedProduct"
            :qrcodeState="1"
          ></qrcode-select-product>
        </el-col>
      </el-form-item>
      <el-form-item :label="`${$t('common.base.customer')}：`">
        <el-col :span="20">
          <biz-search-customer-select
            :placeholder="$t('product.placeholder.customer')"
            :remote-method="fetchCustomer"
            :value="selectCustomer"
            :showLinkMan="true"
            clearable
            @input="customerChange"
          >
          </biz-search-customer-select>
        </el-col>
      </el-form-item>
      <el-form-item :label="`${$t('product.downState')}：`">
        <el-col :span="20">
          <el-select v-model="form.downloads" @change="downloadsChange" clearable>
            <el-option :label="$t('product.hasDownloaded')" :value="1"></el-option>
            <el-option :label="$t('common.base.notDownloaded')" :value="0"></el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item :label="`${$t('product.qrcode.text.text49')}：`">
        <el-col :span="20">
          <UserSearch :value="form.relatedPersons" :remoteMethod="searchUserTag" @update="updateRelatedPersons" :placeholder="$t('product.qrcode.text.text50')"/>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="advance-footer">
      <el-button type="plain-third" @click="resetParams" v-track="$track.formatParams('RESET_SEARCH')">{{ $t('common.base.reset') }}</el-button>
      <el-button type="primary" @click="search" native-type="submit" v-track="$track.formatParams('ADVANCED_SEARCH')">
        {{ $t('common.base.search') }}
      </el-button>
    </div>
  </base-panel>
</template>

<script>
import SelectProduct from './SelectProduct';
import UserSearch from '@src/component/AdvancedSearch/FieldComponents/User.vue';

import { getCustomerList } from '@src/api/CustomerApi'
import { searchUserTag } from '@src/component/AdvancedSearch/advancedSearch.ts';


export default {
  name: 'qrcode-search-form',
  data() {
    return {
      form: {
        productId: '',
        qrcodeId: '',
        customerId: '',
        downloads: '',
        relatedPersons: []
      },
      productList: [],
      advanceFlag: false,
      selectCustomer: [],
      selectedProduct: [],
    };
  },
  methods: {
    searchUserTag,
    open() {
      this.advanceFlag = true;
    },

    selectProduct(val) {
      this.form.productId = val.length ? val[0].id : '';
      this.selectedProduct = val;
      this.$emit('input', 'productId', this.form.productId);
    },

    async fetchCustomer(params) {
      return await getCustomerList(params)
        .then((res) => {
          if (!res || !res.list) return;

          res.list = res.list.map((customer) =>
            Object.freeze({
              label: customer.name,
              value: customer.id,
              ...customer,
            })
          );
          return res;
        })
        .catch((e) => console.error(e));
    },

    customerChange(value) {
      this.selectCustomer = value;
      this.form.customerId = value.length ? value[0].id : '';
      this.$emit('input', 'customerId', this.form.customerId);
    },

    downloadsChange(value) {
      this.form.downloads = value;
      this.$emit('input', 'downloads', this.form.downloads);
    },

    inputChange(val) {
      this.$emit('input', 'qrcodeId', val);
    },
    // 更新关联人
    updateRelatedPersons(value) {
      this.form.relatedPersons = value || [];

      let relateIds = (this.form.relatedPersons || []).map(item => {
        return item.id
      })
      
      this.$emit('input', 'relateUserId', relateIds);
    },

    resetParams() {
      this.$emit('resetParams');
    },

    reset() {
      this.$refs.selectProductRef.clear();
      this.form.qrcodeId = '';
      this.form.productId = '';
      this.form.customerId = '';
      this.form.downloads = '';
      this.selectCustomer = [];
      this.form.relatedPersons = [];
    },
    search() {
      this.$emit('search');
    }
  },
  components: {
    [SelectProduct.name]: SelectProduct,
    UserSearch
  },
};
</script>

<style lang="scss" scoped>
.advanced-search-form {
  overflow: auto;
  padding: 10px 15px 62px;
  height: calc(100% - 51px);

  .el-form-item {
    .el-form-item__content,
    .el-select,
    .base-dist-picker,
    .el-cascader,
    .el-date-editor {
      width: 100%;
    }

    .advanced-search__linkman-search {
      width: 100%;
      ::v-deep .el-select {
        width: 100%;
      }
    }
  }
}

.advance-footer {
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 15px 20px;
}
</style>
